﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using SouthernLand.Common;
using SouthernLand.Common.BOL.NhaDat;
using SouthernLand.Common.Untilities;

namespace SouthernLand.Controls.NhaDat
{
    public partial class TinVip : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Pager.IndexChanged += PageIndex_Changed;
            if (!Page.IsPostBack)
                DataBind_Raovat();
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            rptRaovat.ItemDataBound += rptRaovat_ItemDataBound;
        }

        private void rptRaovat_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var raovat = (Raovat) e.Item.DataItem;
                if (raovat != null)
                {
                    var hplImageView = (HyperLink) e.Item.FindControl("hplImageView");
                    var imgReal = (Image) e.Item.FindControl("imgReal");
                    hplImageView.NavigateUrl = Globals.BuildUrlByRealEstateDetails(raovat.RaovatID,
                                                                                   Globals.ProcessString(
                                                                                       Globals.UnicodeToKoDau(
                                                                                           raovat.Tieude)));
                    imgReal.ImageUrl = Globals.ApplicationPath + "Raovat/Image.ashx?Size=S&ImageUrl=" + raovat.Hinh1 +
                                       "&LoaitinID=" + raovat.Loaitin;
                    imgReal.Attributes.Add("onmouseover",
                                           "showtrail('" + Globals.ApplicationPath +
                                           "Raovat/Image.ashx?Size=L&ImageUrl=" + raovat.Hinh1 + "&LoaitinID=" +
                                           raovat.Loaitin + "','" + raovat.Tieude + "',500,500)");
                    imgReal.Attributes.Add("onmouseout", "hidetrail();");
                    var lblTotalViews = (Label) e.Item.FindControl("lblTotalViews");
                    lblTotalViews.Text = raovat.TotalViews.ToString();
                    var hplRealEstateTitle = (HyperLink) e.Item.FindControl("hplRealEstateTitle");
                    hplRealEstateTitle.Text = raovat.Tieude;
                    hplRealEstateTitle.NavigateUrl = Globals.BuildUrlByRealEstateDetails(raovat.RaovatID,
                                                                                         Globals.ProcessString(
                                                                                             Globals.UnicodeToKoDau(
                                                                                                 raovat.Tieude)));
                }
            }
        }

        protected void dlLoaitin_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataBind_Raovat();
        }

        private void PageIndex_Changed(Object sender, EventArgs e)
        {
            DataBind_Raovat();
        }

        public void DataBind_Raovat()
        {
            Pager.PageSize = 10;
            var objRaovat = new Raovat();
            objRaovat.Loaitin = ConvertUtility.ToInt32(dlLoaitin.SelectedItem.Value);
            objRaovat.CateID = -1;
            objRaovat.Tinhthanh = -1;
            objRaovat.Quanhuyen = -1;
            objRaovat.HuongNha = -1;
            objRaovat.TinhtrangPL = -1;

            List<Raovat> lSearch = Raovat.Search(eTrangthai.TinVip, "", objRaovat, "", -1, "", "", Pager.PageIndex,
                                                 Pager.PageSize);
            rptRaovat.DataSource = lSearch;
            rptRaovat.DataBind();

            int TotalRecords = 0;
            foreach (Raovat f in lSearch)
            {
                TotalRecords = f.TotalRecords;
                break;
            }

            Pager.TotalRecords = CurrentPage.TotalRecords = TotalRecords;
            CurrentPage.TotalPages = Pager.CalculateTotalPages();
            CurrentPage.PageIndex = Pager.PageIndex;

            if (Pager.TotalRecords <= 0)
            {
                PanelMsg.Visible = true;
                lblMsg.Text = "Không có tin rao nào.";
            }
            else
            {
                PanelMsg.Visible = false;
            }

            // kiem tra xem co can hien thi phan trang ko
            DisplayPager.Visible = Pager.TotalRecords > Pager.PageSize;
        }
    }
}